package com.songsong.myblog2.service.impl;

import com.songsong.myblog2.domain.Tag;
import com.songsong.myblog2.mapper.TagMapper;
import com.songsong.myblog2.service.TagService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "tagService")
public class TagServiceImpl implements TagService {
    /*注入tagMapper*/
    @Resource(name = "tagMapper")
    private TagMapper tagMapper;

    /**
     * 添加标签
     *
     * @param tagName
     */
    @Override
    public void addTag(String tagName) {
        tagMapper.addTag(tagName);
    }

    /**
     * 查找标签
     *
     * @param tagName
     * @return
     */
    @Override
    public String selectTag(String tagName) {
        String selectTag = tagMapper.selectTag(tagName);
        return selectTag;
    }

    /**
     * 更新标签
     *
     * @param tagName
     */
    @Override
    public void updateTagNum(String tagName) {
        tagMapper.updateTagNum(tagName);
    }

    /**
     * 获取标签数量
     *
     * @return
     */
    @Override
    public int getTagNum() {
        int num = tagMapper.getTagNum();
        return num;
    }

    /**
     * 获取标签云
     *
     * @return
     */
    @Override
    public JSONObject findTagsCloud() {
        List<Tag> tags = tagMapper.findTagsCloud();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 200);
        jsonObject.put("result", JSONArray.fromObject(tags));
        jsonObject.put("tagsNum", tags.size());
        return jsonObject;
    }
}
